package blockingqueueDemo;

import java.util.concurrent.atomic.AtomicInteger;

public class TestMyBlockingQ {
    static AtomicInteger counter = new AtomicInteger(0);
    static MyBlockingQueue blockingQ = new MyBlockingQueueObjectWait(30);

    static class ProducerTask implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10000; i++) {
                try {
                    blockingQ.put(String.valueOf(i) + " written by " + Thread.currentThread().getName());
                    System.out.println("Put " + i + " in queue with " + Thread.currentThread().getName());
                    System.out.println("The queue is now the size of :" + blockingQ.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class ConsumerTask implements Runnable {


        @Override
        public void run() {
            for (int i = 0; i < 10000; i++) {
                try {
                    System.out.println("The element got from the queue is : " + blockingQ.get());
                    counter.getAndIncrement();
                    System.out.println("The total elements read from the queue is :" + counter + " with " + Thread.currentThread().getName());
                    System.out.println("The queue is now the size of :" + blockingQ.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 30; i++) {
            new Thread(new ProducerTask()).start();
        }
        for (int i = 0; i < 30; i++) {
            new Thread(new ConsumerTask()).start();
        }
    }
}
